<?php
set_time_limit(90);
//sleep(90);
error_reporting(E_ALL);
//error_reporting(E_ERROR);

define('PATH_BASE', dirname(__FILE__));
require_once('config/config.php');

set_include_path(PATH_BASE . PATH_SEPARATOR . PATH_BASE. '/library/'
. PATH_SEPARATOR . PATH_BASE. '/library/PEAR'
. PATH_SEPARATOR . PATH_BASE.'/application/models/'
. PATH_SEPARATOR . PATH_BASE.'/library/PEAR/'
. PATH_SEPARATOR . get_include_path() );

include "Zend/Loader.php";

Zend_Loader::registerAutoload();

if ( (!empty($_REQUEST["PHPSESSID"]) && !empty($_REQUEST["PHPSESSIDX"]))
&& $_REQUEST["PHPSESSID"] != $_REQUEST["PHPSESSIDX"] ) {

	$_REQUEST["PHPSESSID"] = $_REQUEST["PHPSESSIDX"];
	unset($_REQUEST["PHPSESSIDX"]);
	$_COOKIE["PHPSESSID"] = $_REQUEST["PHPSESSID"];
}

$time = 60*60*24*7; /*= 31536000 */
Zend_Session::setOptions(array('cookie_lifetime' => $time, 'gc_maxlifetime' => $time));
Zend_Session::start();
Zend_Session::setOptions(array('strict' => true));

$config = new Zend_Config_Xml(PATH_BASE.'/config/config.xml', 'common');

// load registry
$registry = Zend_Registry::getInstance();

// add configuration to registry
$registry->set('config', $config);

// setup database
$db = Zend_Db::factory($config->database->adapter, $config->database->config->toArray());
//$db->getConnection();
Zend_Db_Table_Abstract::setDefaultAdapter($db);

$db->setFetchMode(Zend_Db::FETCH_ASSOC);
$db->query("SET CHARSET utf8");

$frontController = Zend_Controller_Front::getInstance();
$frontController->throwExceptions(true);

$router = new Zend_Controller_Router_Rewrite();
$route = new Zend_Controller_Router_Route(
':controller/:action/:id/:page',
array(
'page' => 0,
'id'       => 0,
'controller' => 'news',
'action'     => 'index',
'module'	=> 'client'
),
array(
'id' => '(\d+|all)',
'page' => '\d+'
)
);
$router->addRoute('route', $route);

$routePage = new Zend_Controller_Router_Route(
'page/:id',
array(
'id'       => 0,
'controller' => 'page',
'action'     => 'index',
'module'	=> 'client'
),
array( 'id' => '\d+', 'page' => '\d+')
);
$router->addRoute('routePage', $routePage);
/*
$routeAdmin2 = new Zend_Controller_Router_Route(
'admin/:controller/:action/:id/',
array(
'id'         => 0,
'controller' => 'index',
'action'     => 'index',
'module'	 => 'admin'
),
array( 'id' => '\d+')
);


$router->addRoute('routeAdmin', $routeAdmin2);
*/
$routeAdmin = new Zend_Controller_Router_Route(
'admin/:controller/:action/:id/:id_agent/:query',
array(
'id'         => 0,
'id_agent' => 0,
'query' => '',
'controller' => 'index',
'action'     => 'index',
'module'	 => 'admin'
),
array( 'id' => '\d+', 'id_agent' => '\d+')
);


$router->addRoute('routeAdmin', $routeAdmin);

$routeOperator = new Zend_Controller_Router_Route(
'operator/:controller/:action/:id',
array(
'id'         => 0,
'controller' => 'index',
'action'     => 'index',
'module'	 => 'operator'
),
array( 'id' => '\d+')
);


$router->addRoute('routeOperator', $routeOperator);

$routeSupervisor = new Zend_Controller_Router_Route(
'supervisor/:controller/:action/:id',
array(
'id'         => 0,
'controller' => 'index',
'action'     => 'index',
'module'	 => 'supervisor'
),
array( 'id' => '\d+')
);


$router->addRoute('routeSupervisor', $routeSupervisor);

$frontController->setRouter($router);

// Регистрируем плагин
$frontController->registerPlugin(new FRM_Controller_Plugin_Standart());

$frontController->dispatch();